<?php 
	class Service extends CI_Controller {
		
		function __construct()
		{
			parent::__construct();
			
			$this->load->model('Jobs');
			$this->load->model('Chats');
			$this->load->helper('url');
			$this->load->helper('form');
			$this->load->model('Company');
			$this->load->model('Rinvoices');			
			$this->Simulate =0;
		}
		public function index() {
			redirect('/');
		}
		/*_______________________Luan van's Code___________________________*/
		
		
	
		
		
		
		
		
		
		
		/*______________________Bookabus's code________________________*/
		
		// send mail to user to reset password
		function forgot_password()
		{   		
		if($this->Simulate==1){
			echo'{"status":"ok","result":{"message":"An email reset password sent to your email"}}';
		}
		else{
			header('Content-type: application/json');
			$result= false;
			if (@$_REQUEST['email']) {
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');
				
				// intial email
				$mailconf['mailtype'] = 'html';
				$this->email = new CI_Email($mailconf);
				
				$menu = new Menu;
				$user =& Users::getInstance();
				
				$data = $menu->gen();
				
				$email = @$_REQUEST['email'];
				//die($email);
				$this->db->where('email',$email);
				$q = $this->db->get('users');
				$user = $q->row();
				if ($user) {
					$password = substr(md5(time()), 0, 6);
					$this->db->where('uid', $user->uid);
					$this->db->update('users',array(
						'password'		=>	do_hash($password, 'md5')
					));
					
					$this->email->from('ivy@bookabus.sg', 'Ivy');
					$this->email->to($user->email);
					
					$this->email->subject('Bookabus.sg - Password Retreival');
					$this->email->message('Hello, '.$user->name.',
						<br /><br/>
						You have requested a password retrieval.
						<br /><br/>
						Your password is: '.$password.'
						<br />You can sign in your account {unwrap}'.anchor('/sign-in', 'here').'{/unwrap}.
						<br /><b r/>
						If you haven\'t authorised this password retrieval, please contact us.
						<br /><br />
						Best Regards,
						<br />Ivy');
					
					$this->email->send();
					//echo $this->email->print_debugger();
					
					$result = true;
				}	
				if($result==true){
					$arr = array ('status'=>'ok','result'=>array('message'=>'An email reset password sent to '.$email));
					echo json_encode($arr);
				}
			}
			
			if ($result!=true){
					$arr = array ('status'=>'false','result'=>array('message'=>'Email invalid!'));
					echo json_encode($arr);
			}	
		}
		}
		
		//get all job relate to an user
		function get_all_draft()
		{   
		if($this->Simulate==1){
			echo '{"type":"Trip Base","start_date":"2012-12-29 14:34:00","passangers":"6","from":"6","to":"["6"]","return":"6","return_time":"2012-12-31 14:35:00"}{"type":"Time Base","start_date":"2012-12-29 14:38:00","end_date":"2012-12-30 17:38:00","passangers":"8","locations":"["8"]","hours":"27"}{"type":"Time Base","start_date":"2012-12-29 14:38:00","end_date":"2012-12-30 17:38:00","passangers":"8","locations":"["8"]","hours":"27"}{"type":"Trip Base","start_date":"2012-12-29 14:34:00","passangers":"6","from":"6","to":"["6"]","return":"6","return_time":"2012-12-31 14:35:00"}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if (@$_REQUEST['userid']) {
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				$userid = @$_REQUEST['userid'];
				if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `users` WHERE `uid`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$userid = @$_REQUEST['userid'];
						$sql = "SELECT `id`,`start_date`,`end_date`,`passangers`,`from`,`to`,`locations`,`return`,`return_time`,`hours`,`job_type` FROM `jobs` WHERE `owner_id`=$userid "; //AND `start_date`< NOW()
						$elapse = $this->db->query($sql);	
						$trips= $elapse->result();
						$count_elapse = count($trips);
						
						if($count_elapse >0){
							for($i=0; $i<$count_elapse; $i++){
								$id = $trips[$i];
								if($id->job_type == 0 ){
									$arr[$i] = array ('type'=>'Trip Base','start_date'=>$id->start_date,'passangers'=>$id->passangers,'from'=>$id->from,'to'=>$id->to,'return'=>$id->return,'return_time'=>$id->return_time,);
								}
								else if($id->job_type == 1 ){
									$arr[$i] = array ('type'=>'Time Base','start_date'=>$id->start_date,'end_date'=>$id->end_date,'passangers'=>$id->passangers,'locations'=>$id->locations,'hours'=>$id->hours);
								}
								else if($id->job_type == 2 ){
									$arr[$i] = array ('type'=>'Term Base','start_date'=>$id->start_date);
								}
							}
							$string = json_encode($arr);
							$string = str_replace( '\"','"' , $string);
							$string = str_replace( '"[','[' , $string);
							$string = str_replace( ']"',']' , $string);
							echo ('{"List job": '.$string.'}');
						}
						else{
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID have to be an integer'));
					echo json_encode($arr);
				}
			}
		}
		}
		// get all completed jobs
		function get_all_complete_job()
		{  
		if($this->Simulate==1){
			echo '{"type":"Time Base","status":"complete","start_date":"2013-01-03 10:02:00","end_date":"2013-01-12 13:02:00","passangers":"3","locations":"["asd"]","hours":"219"}{"type":"Trip Base","status":"complete","start_date":"2013-01-02 10:02:00","passangers":"3","from":"3","to":"["3"]","return":"3","return_time":"2013-01-03 10:02:00"}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if (@$_REQUEST['userid']) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				$userid = @$_REQUEST['userid'];
					if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `users` WHERE `uid`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){		
						$userid = @$_REQUEST['userid'];
						$sql = "SELECT `id`,`start_date`,`end_date`,`passangers`,`from`,`to`,`locations`,`return`,`return_time`,`hours`,`job_type`,`job_status` FROM `jobs` WHERE `owner_id`=$userid AND `job_status`=7 "; //AND `start_date`< NOW()
						$elapse = $this->db->query($sql);	
						$trips= $elapse->result();
						$count_elapse = count($trips);
						
						if($count_elapse >0){
							for($i=0; $i<$count_elapse; $i++){
								$id = $trips[$i];
								if($id->job_type == 0 ){
									$arr[$i] = array ('type'=>'Trip Base','status'=>'complete','start_date'=>$id->start_date,'passangers'=>$id->passangers,'from'=>$id->from,'to'=>$id->to,'return'=>$id->return,'return_time'=>$id->return_time,);
								}
								else if($id->job_type == 1 ){
									$arr[$i] = array ('type'=>'Time Base','status'=>'complete','start_date'=>$id->start_date,'end_date'=>$id->end_date,'passangers'=>$id->passangers,'locations'=>$id->locations,'hours'=>$id->hours);
								}
								else if($id->job_type == 2 ){
									$arr[$i] = array ('type'=>'Term Base','status'=>'complete','start_date'=>$id->start_date);
								}
							}
							$string = json_encode($arr);
							$string = str_replace( '\"','"' , $string);
							$string = str_replace( '"[','[' , $string);
							$string = str_replace( ']"',']' , $string);
							echo ('{"List job": '.$string.'}');
							
						}
						else{
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID have to be an integer'));
					echo json_encode($arr);
				}
			}
		}
		}
		// get awating con firm job
		function get_awating_confirm_job()
		{  
		if($this->Simulate==1){
			echo '{"Group_ID":"2","Owner_ID":"134","status":"awating confirm","start_date":"2013-03-15 11:04:00","end_date":"2013-03-25 11:04:00","elapsed_date":"51","price":"60","payment_method":"["cheque"]"}
{"Group_ID":"3","Owner_ID":"134","status":"oncoming","start_date":"2013-04-25 11:15:00","end_date":"2013-05-29 11:15:00","elapsed_date":"69","price":"45","payment_method":"["paypal"]"}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if (@$_REQUEST['userid']) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				$userid = @$_REQUEST['userid'];
					if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `users` WHERE `uid`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){		
						$userid = @$_REQUEST['userid'];
						$sql = "SELECT * FROM `job_group` WHERE `owner_id`=$userid AND `status` = 1 AND `draft` = 0 AND (`end_date` > NOW()OR `end_date` =0) "; //AND `start_date`< NOW()
						$elapse = $this->db->query($sql);	
						$trips= $elapse->result();
						$count_elapse = count($trips);
						
						if($count_elapse >0){
							for($i=0; $i<$count_elapse; $i++){
								$id = $trips[$i];
								$arr[$i] = array ('Group_ID'=>$id->gid,'Owner_ID'=>$id->owner_id,'status'=>'awating confirm','start_date'=>$id->start_date,'end_date'=>$id->end_date,'elapsed_date'=>$id->elapsed_date,'price'=>$id->price,'payment_method'=>$id->pmethods,);
							}
							$string = json_encode($arr);
							$string = str_replace( '\"','"' , $string);
							$string = str_replace( '"[','[' , $string);
							$string = str_replace( ']"',']' , $string);
							echo ('{"List job": '.$string.'}');
						}
						else{
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID have to be an integer'));
					echo json_encode($arr);
				}
			}
		}
		}
		// get oncoming job
		function get_all_oncoming_job()
		{  
		if($this->Simulate==1){
			echo '{"Group_ID":"2","Owner_ID":"134","status":"oncoming","start_date":"2013-03-15 11:04:00","end_date":"2013-03-25 11:04:00","elapsed_date":"51","price":"60","payment_method":"["cheque"]"}
{"Group_ID":"3","Owner_ID":"134","status":"oncoming","start_date":"2013-04-25 11:15:00","end_date":"2013-05-29 11:15:00","elapsed_date":"69","price":"45","payment_method":"["paypal"]"}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if (@$_REQUEST['userid']) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				$userid = @$_REQUEST['userid'];
					if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `users` WHERE `uid`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){		
						$userid = @$_REQUEST['userid'];
						$sql = "SELECT * FROM `job_group` WHERE `owner_id`=$userid AND ((`status` <= 6 AND `status`>=0) OR `status`=13 OR `status`=14) AND `draft` = 0 AND (`end_date` > NOW()OR `end_date` =0) "; //AND `start_date`< NOW()
						$elapse = $this->db->query($sql);	
						$trips= $elapse->result();
						$count_elapse = count($trips);
						
						if($count_elapse >0){
							for($i=0; $i<$count_elapse; $i++){
								$id = $trips[$i];
								$arr[$i] = array ('Group_ID'=>$id->gid,'Owner_ID'=>$id->owner_id,'status'=>'oncoming','start_date'=>$id->start_date,'end_date'=>$id->end_date,'elapsed_date'=>$id->elapsed_date,'price'=>$id->price,'payment_method'=>$id->pmethods,);
								
							}
							$string = json_encode($arr);
							$string = str_replace( '\"','"' , $string);
							$string = str_replace( '"[','[' , $string);
							$string = str_replace( ']"',']' , $string);
							echo ('{"List job": '.$string.'}');
						}
						else{
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID have to be an integer'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		//get all due and overdue payment
		function get_all_payment_due()
		{   
		if($this->Simulate==1){
			echo '{"type":"Trip Base","start_date":"2013-01-09 10:41:00","passangers":"2","from":"3","to":"["4"]","return":"3","return_time":"2013-01-10 10:41:00","payment_due_date":"7","payment_overdue":"1"}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if (@$_REQUEST['userid']) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid = @$_REQUEST['userid'];
				if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `user` WHERE `id`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						
							echo ('yes');
							
						
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID have to be an integer'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		// get all conversation about a job group
		function get_conversations()
		{   
		if($this->Simulate==1){
			echo '{"id":"29","jid":"2","uid":"8","text":"Order deleted by admin.","postdate":"2012-11-14 14:07:21","type":"0","paid":"0","payment":"0"}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if ((@$_REQUEST['userid'])&&(@$_REQUEST['jobgroupid'])) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid = @$_REQUEST['userid'];
				$jobid = @$_REQUEST['jobgroupid'];
				if((preg_match("/^[0-9]+$/", $userid))&&(preg_match("/^[0-9]+$/", $jobid))){
					$sql = "SELECT * FROM `conversation` WHERE `uid`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "SELECT * FROM `conversation` WHERE `jid`=$jobid ";
						$elapse2 = $this->db->query($sql);	
						$job_flag= $elapse2->result();
						$count_elapse2 = count($job_flag);
						if($count_elapse2 >0){
							$sql = "SELECT * FROM `conversation` WHERE `uid`=$userid AND `jid`=$jobid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);	
							$trips= $elapse->result();
							$count_elapse = count($trips);
							if($count_elapse >0){
								for($i=0; $i<$count_elapse; $i++){
									$id = $trips[$i];
									$arr[$i] = $id;
									$strings = json_encode($arr[$i]);
									$strings = str_replace( '\t', '	', $strings );
									$strings = str_replace( '\n', '
	', $strings );
									$strings = str_replace( '\/', '/', $strings );
									
									echo $strings;
									echo "\n";
								}
								
							}
							else{
								$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no conversation!'));
								echo json_encode($arr);
							}
						}
						else{
							$arr = array ('Status'=>'false','result'=>array('message'=>'JobGroupID invalid!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		//deny a quote in job group 
		function deny_quote_job_group()
		{ 
		if($this->Simulate==1){
			echo '{"Status":"ok","result":{"message":"Have already cancelled this quote."}}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if ((@$_REQUEST['userid'])&&(@$_REQUEST['jobgroupid'])) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid = @$_REQUEST['userid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if((preg_match("/^[0-9]+$/", $userid))&&(preg_match("/^[0-9]+$/", $jobgroupid))){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "SELECT * FROM `job_group` WHERE `gid`=$jobgroupid ";
						$elapse2 = $this->db->query($sql);	
						$job_flag= $elapse2->result();
						$count_elapse2 = count($job_flag);
						if($count_elapse2 >0){
							$sql = "UPDATE `job_group` SET `status`=8 WHERE `owner_id`=$userid AND `gid`=$jobgroupid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);
							$sql = "UPDATE `jobs` SET `job_status`=8 WHERE `owner_id`=$userid AND `belongs`=$jobgroupid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have already cancelled this quote.'));
							echo json_encode($arr);
						}
						else{
							$arr = array ('Status'=>'false','result'=>array('message'=>'JobGroupID invalid!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		// accept a quote in job group
		function accept_quote_job_group()
		{   
		if($this->Simulate==1){
			echo'{"Status":"ok","result":{"message":"Have already accepted this quote."}}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			if ((@$_REQUEST['userid'])&&(@$_REQUEST['jobgroupid'])) {
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid = @$_REQUEST['userid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if((preg_match("/^[0-9]+$/", $userid))&&(preg_match("/^[0-9]+$/", $jobgroupid))){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "SELECT * FROM `job_group` WHERE `gid`=$jobgroupid ";
						$elapse2 = $this->db->query($sql);	
						$job_flag= $elapse2->result();
						$count_elapse2 = count($job_flag);
						if($count_elapse2 >0){
							$sql = "UPDATE `job_group` SET `status`=4 WHERE `owner_id`=$userid AND `gid`=$jobgroupid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);	
							$sql = "UPDATE `jobs` SET `job_status`=4 WHERE `owner_id`=$userid AND `belongs`=$jobgroupid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);	
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have already accepted this quote.'));
							echo json_encode($arr);
						}
						else{
							$arr = array ('Status'=>'false','result'=>array('message'=>'JobGroupID invalid!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		
		//cancel a job group
		function cancel_job_group()
		{   
		if($this->Simulate==1){
			echo '{"Status":"ok","result":{"message":"Have already deleted this quote."}}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			$userid = @$_REQUEST['userid'];
			if ((@$_REQUEST['userid'])&&(@$_REQUEST['jobgroupid'])) {
				
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid = @$_REQUEST['userid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if((preg_match("/^[0-9]+$/", $userid))&&(preg_match("/^[0-9]+$/", $jobgroupid))){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "SELECT * FROM `job_group` WHERE `gid`=$jobgroupid ";
						$elapse2 = $this->db->query($sql);	
						$job_flag= $elapse2->result();
						$count_elapse2 = count($job_flag);
						if($count_elapse2 >0){
							$sql = "DELETE FROM `job_group` WHERE `owner_id`=$userid AND `gid`=$jobgroupid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);	
							$sql = "DELETE FROM `jobs` WHERE `owner_id`=$userid AND `belongs`=$jobgroupid"; //AND `start_date`< NOW()
							$elapse = $this->db->query($sql);
							$arr = array ('Status'=>'ok','result'=>array('message'=>'Have already deleted this quote.'));
							echo json_encode($arr);
						}
						else{
							$arr = array ('Status'=>'false','result'=>array('message'=>'JobGroupID invalid!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		// delete a job
		function delete_time_base()
		{   
		if($this->Simulate==1){
			echo'{"Status":"ok","result":{"message":"Have already deleted this job."}}';
		}
		else{
			header('Content-type: application/json');
			$arr;
			$userid = @$_REQUEST['userid'];
			if ((@$_REQUEST['userid'])&&(@$_REQUEST['jobgroupid'])&&(@$_REQUEST['jobid'])) {
				
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid = @$_REQUEST['userid'];
				$jobid = @$_REQUEST['jobid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if((preg_match("/^[0-9]+$/", $userid))&&(preg_match("/^[0-9]+$/", $jobgroupid))&&(preg_match("/^[0-9]+$/", $jobid))){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=$userid ";
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "SELECT * FROM `job_group` WHERE `gid`=$jobgroupid ";
						$elapse2 = $this->db->query($sql);	
						$jobgroup_flag= $elapse2->result();
						$count_elapse2 = count($jobgroup_flag);
						if($count_elapse2 >0){
							$sql = "SELECT * FROM `jobs` WHERE `id`=$jobid ";
							$elapse3 = $this->db->query($sql);	
							$job_flag= $elapse3->result();
							$count_elapse3 = count($job_flag);
							if($count_elapse3 >0){
								$sql = "DELETE FROM `jobs` WHERE `owner_id`=$userid AND `belongs`=$jobgroupid AND `id`=$jobid"; //AND `start_date`< NOW()
								$elapse = $this->db->query($sql);	
								$sql = "SELECT * FROM `jobs` WHERE `belongs`=$jobgroupid ";
								$elapse4 = $this->db->query($sql);
								$jobgroup1_flag= $elapse4->result();	
								$count_elapse4 = count($jobgroup1_flag);
								if($count_elapse4 > 0){	
								}
								else{
									$sql = "DELETE FROM `job_group` WHERE `owner_id`=$userid AND `gid`=$jobgroupid"; //AND `start_date`< NOW()
									$elapse = $this->db->query($sql);
								}
								$arr = array ('Status'=>'ok','result'=>array('message'=>'Have already deleted this job.'));
								echo json_encode($arr);
							}
							else{
								$arr = array ('Status'=>'false','result'=>array('message'=>'JobID invalid!'));
								echo json_encode($arr);
							}
						}
						else{
							$arr = array ('Status'=>'false','result'=>array('message'=>'JobGroupID invalid!'));
							echo json_encode($arr);
						}
					}
					else{
						$arr = array ('Status'=>'false','result'=>array('message'=>'UserID invalid!'));
						echo json_encode($arr);
					}
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}

		//add new time base
		function add_time_base()
		{  
		if($this->Simulate==1){
			echo'{"id":"285","type":"Time Base","start_date":"2013-11-15 18:30:13","end_date":"2013-11-13 19:30:13","passangers":"1234","locations":"["7"]","hours":"24"}{"Status":"ok","result":{"message":"Job has been add to database!"}}';
		}
		else{
			header('Content-type: application/json');
			/*$body_request = (string)('{
										"requestBody": [
											{
												"type": "timeBase",
												"startDate": "2012-12-12 14:05:00",
												"endDate": "2012-12-30 17:30:00",
												"numberOfPassengers": 5,
												"locations": [
													"Di An",
													"Binh Duong",
													"TPHCM"
												]
											}
										]
									}');*/
			$body_request = file_get_contents('php://input');
			$temp = explode('{',$body_request);
			$temp1 = explode('}',$temp[2]);
			$temp2 = explode('"',$temp1[0]);
			$startdate = $temp2[7];
			$enddate   = $temp2[11];
			$temp3 = explode(' ',$temp2[14]);
			$temp4 = explode(',',$temp3[1]);
			$numofpas  = $temp4[0];
			$temp5 = explode('[',$temp1[0]);
			$locations = '['.$temp5[1];
			$locations = str_replace("\t","",$locations);
			$locations = str_replace("\r","",$locations);
			$locations = str_replace("\n","",$locations);
			$numofhour = $this->diffhour($startdate,$enddate);
			$num = count($locations);
			$location = '[';
			for ($i = 1; $i <= $num; $i++) {
				if ($i==$num){
					$location = $location.'"'.$locations[$i-1].'"';
				}
				else{
					$location = $location.'"'.$locations[$i-1].'"'.',';
				}
			}
			$location = $location.']';
			$arr;
			/*$startdate = @$_REQUEST['startDate'];
			$enddate   = @$_REQUEST['endDate'];
			$numofpas  = @$_REQUEST['numberOfPassengers'];
			$numofhour = $this->diffhour($startdate,$enddate);
			$locations = @$_REQUEST['locations'];*/
			if (true){
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid =@$_REQUEST['userid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=".$userid ." AND `gid`=".$jobgroupid;
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "INSERT INTO `jobs` (`start_date`,`end_date`,`passangers`,`hours`,`locations`,`owner_id`,`job_type`,`belongs`) VALUES ('$startdate','$enddate',".$numofpas.",".$numofhour.",'$locations',".$userid.",1,".$jobgroupid.")" ;
						$elapse = $this->db->query($sql);	
						
					}
					else{
						$sql = "INSERT INTO `jobs` (`start_date`,`end_date`,`passangers`,`hours`,`locations`,`owner_id`,`job_type`,`belongs`) VALUES ('$startdate','$enddate',".$numofpas.",".$numofhour.",'$locations',".$userid.",1,".$jobgroupid.")" ;
						$elapse = $this->db->query($sql);
						$sql = "INSERT INTO `job_group` (`start_date`,`end_date`,`owner_id`,`gid`) VALUES ('$startdate','$enddate',".$userid.",".$jobgroupid.")" ;
						$elapse1 = $this->db->query($sql);						
					}
					$sql = "SELECT `id` FROM `jobs` ORDER BY `id` DESC";
					$elapse = $this->db->query($sql);	
					$trips= $elapse->result();
					$temp = $trips[0]->id;
					$sql = "SELECT `id`,`start_date`,`end_date`,`passangers`,`from`,`to`,`locations`,`return`,`return_time`,`hours`,`job_type` FROM `jobs` WHERE `id`=$temp "; //AND `start_date`< NOW()
					$elapse = $this->db->query($sql);	
					$trips= $elapse->result();
					$count_elapse = count($trips);
						
					if($count_elapse >0){
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Job has been added to database!'));
						echo json_encode($arr);
						for($i=0; $i<$count_elapse; $i++){
							$id = $trips[$i];
							if($id->job_type == 0 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Trip Base','start_date'=>$id->start_date,'passangers'=>$id->passangers,'from'=>$id->from,'to'=>$id->to,'return'=>$id->return,'return_time'=>$id->return_time,);
							}
							else if($id->job_type == 1 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Time Base','start_date'=>$id->start_date,'end_date'=>$id->end_date,'passangers'=>$id->passangers,'locations'=>$id->locations,'hours'=>$id->hours);
							}
							else if($id->job_type == 2 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Term Base','start_date'=>$id->start_date);
							}
						}
						$string = json_encode($arr);
						$string = str_replace( '\"','"' , $string);
						$string = str_replace( '"[','[' , $string);
						$string = str_replace( ']"',']' , $string);
						echo ('{"List job": '.$string.'}');
								
					}
					else{
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
						echo json_encode($arr);
					}
					
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}

		// edit time base
		function edit_time_base()
		{   
		if($this->Simulate==1){
			echo'{"id":"236","type":"Time Base","start_date":"2013-11-15 18:30:13","end_date":"2013-11-13 19:30:13","passangers":"5","locations":"["7"]","hours":"6"}{"Status":"ok","result":{"message":"Job has been add to database!"}}';
		}
		else{
			header('Content-type: application/json');
			/*$body_request = (string)('{
										"requestBody": [
											{
												"type": "timeBase",
												"startDate": "2012-12-12 12:00:00",
												"endDate": "2012-12-30 12:00:00",
												"numberOfPassengers": 5,
												"locations": [
													"Di An",
													"Binh Duong",
													"TPHCM"
												]
											}
										]
									}');*/
			$body_request = file_get_contents('php://input');
			$temp = explode('{',$body_request);
			$temp1 = explode('}',$temp[2]);
			$temp2 = explode('"',$temp1[0]);
			$startdate = $temp2[7];
			$enddate   = $temp2[11];
			$temp3 = explode(' ',$temp2[14]);
			$temp4 = explode(',',$temp3[1]);
			$numofpas  = $temp4[0];
			$temp5 = explode('[',$temp1[0]);
			$locations = '['.$temp5[1];
			$locations = str_replace("\t","",$locations);
			$locations = str_replace("\r","",$locations);
			$locations = str_replace("\n","",$locations);
			$numofhour = $this->diffhour($startdate,$enddate);
			$num = count($locations);
			$location = '[';
			for ($i = 1; $i <= $num; $i++) {
				if ($i==$num){
					$location = $location.'"'.$locations[$i-1].'"';
				}
				else{
					$location = $location.'"'.$locations[$i-1].'"'.',';
				}
			}
			$location = $location.']';
			/*$startdate = @$_REQUEST['startDate'];
			$enddate   = @$_REQUEST['endDate'];
			$numofpas  = @$_REQUEST['numberOfPassengers'];
			$numofhour = $this->diffhour($startdate,$enddate);
			$locations = @$_REQUEST['locations'];*/
			$arr;
			if (true){
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid =@$_REQUEST['userid'];
				$jobid =@$_REQUEST['jobid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=".$userid ." AND `gid`=".$jobgroupid;
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					$sql = "SELECT * FROM `jobs` WHERE `owner_id`=".$userid ." AND `belongs`=".$jobgroupid." AND `id`=".$jobid." AND `job_type`=1";
					$elapse2 = $this->db->query($sql);	
					$user_flag2= $elapse2->result();
					$count_elapse2 = count($user_flag2);
					if($count_elapse1 >0){
						if($count_elapse2 >0){
							$sql = "UPDATE `jobs` SET `start_date`='$startdate',`end_date`='$enddate',`passangers`=".$numofpas.",`hours`=".$numofhour.",`locations`='$locations',`owner_id`=".$userid.",`belongs`=".$jobgroupid." WHERE `id`=".$jobid;
							$elapse = $this->db->query($sql);	
						}
						else{
							$arr = array ('Status'=>'False','result'=>array('message'=>'This is not a time base. Try with another JobID'));
							echo json_encode($arr);	
							return 0;
						}
						
					}
					else{
						$arr = array ('Status'=>'False','result'=>array('message'=>'UserID or JobID or JobGroupID is not correct!'));
						echo json_encode($arr);			
					}
					
					$sql = "SELECT `id`,`start_date`,`end_date`,`passangers`,`from`,`to`,`locations`,`return`,`return_time`,`hours`,`job_type` FROM `jobs` WHERE `id`=".$jobid; //AND `start_date`< NOW()
					$elapse = $this->db->query($sql);	
					$trips= $elapse->result();
					$count_elapse = count($trips);
						
					if($count_elapse >0){
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Job has been updated to database!'));
						echo json_encode($arr);	
						for($i=0; $i<$count_elapse; $i++){
							$id = $trips[$i];
							if($id->job_type == 0 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Trip Base','start_date'=>$id->start_date,'passangers'=>$id->passangers,'from'=>$id->from,'to'=>$id->to,'return'=>$id->return,'return_time'=>$id->return_time,);
							}
							else if($id->job_type == 1 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Time Base','start_date'=>$id->start_date,'end_date'=>$id->end_date,'passangers'=>$id->passangers,'locations'=>$id->locations,'hours'=>$id->hours);
							}
							else if($id->job_type == 2 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Term Base','start_date'=>$id->start_date);
							}
						}	
						$string = json_encode($arr);
						$string = str_replace( '\"','"' , $string);
						$string = str_replace( '"[','[' , $string);
						$string = str_replace( ']"',']' , $string);
						echo ('{"List job": '.$string.'}');
					}
					else{
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
						echo json_encode($arr);
					}
					
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
			
		}
		}
		
		// add new trip base
		function add_trip_base()
		{   
		if($this->Simulate==1){
			echo '{"id":"284","type":"Trip Base","start_date":"2013-11-15 18:30:13","passangers":"1234","from":"24","to":"["4"]","return":"23","return_time":"2013-11-16 13:35:34"}{"Status":"ok","result":{"message":"Job has been add to database!"}}';
		}
		else{
			header('Content-type: application/json');
			/*$body_request =(string)('{
				"requestBody": [
					{
						"type": "tripBase",
						"startDate": "2012-12-30 12:00:00",
						"numberOfPassengers": 5,
						"from": "Binh Duong",
						"to": [
							"Vung Tau",
							"Nha Trag",
							"TPHCM"
						],
						"returnLocation": "Binh Duong",
						"returnTime": "2012-12-31 12:00:00"
					}
				]
			}');*/
			$body_request = file_get_contents('php://input');
			$temp = explode('{',$body_request);
			$temp1 = explode('}',$temp[2]);
			$temp2 = explode('"',$temp1[0]);
			$startdate = $temp2[7];
			$temp3 = explode(' ',$temp2[10]);
			$temp4 = explode(',',$temp3[1]);
			$numofpas  = $temp4[0];
			$from= $temp2[13];
			$temp5 = explode('[',$temp1[0]);
			$temp6 = explode(']',$temp5[1]);
			$locations = '['.$temp6[0].']';
			$locations = str_replace("\t","",$locations);
			$locations = str_replace("\r","",$locations);
			$to = str_replace("\n","",$locations);
			$temp7 = explode('"',$temp6[1]);
			$returnplace   = $temp7[3];
			$returntime   = $temp7[7];
			/*$startdate = @$_REQUEST['startDate'];
			$returntime  = @$_REQUEST['returnTime'];
			$numofpas  = @$_REQUEST['numberOfPassengers'];
			$from = @$_REQUEST['from'];
			$to = @$_REQUEST['to'];
			$returnplace = @$_REQUEST['returnLocation'];*/
			$arr;
			if (true){
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid =@$_REQUEST['userid'];
				$jobgroupid =@$_REQUEST['jobgroupid'];
				if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=".$userid ." AND `gid`=".$jobgroupid;
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					if($count_elapse1 >0){
						$sql = "INSERT INTO `jobs` (`start_date`,`return_time`,`passangers`,`from`,`to`,`owner_id`,`job_type`,`belongs`,`return`) VALUES ('$startdate','$returntime',".$numofpas.",'$from','$to',".$userid.",0,".$jobgroupid.",'$returnplace')" ;
						$elapse = $this->db->query($sql);	
						
					}
					else{
						$sql = "INSERT INTO `jobs` (`start_date`,`return_time`,`passangers`,`from`,`to`,`owner_id`,`job_type`,`belongs`,`return`) VALUES ('$startdate','$returntime',".$numofpas.",'$from','$to',".$userid.",0,".$jobgroupid.",'$returnplace')" ;
						$elapse = $this->db->query($sql);	
						$sql = "INSERT INTO `job_group` (`start_date`,`owner_id`,`gid`) VALUES ('$startdate',".$userid.",".$jobgroupid.")" ;
						$elapse1 = $this->db->query($sql);						
					}
					$sql = "SELECT `id` FROM `jobs` ORDER BY `id` DESC";
					$elapse = $this->db->query($sql);	
					$trips= $elapse->result();
					$temp = $trips[0]->id;
					$sql = "SELECT `id`,`start_date`,`end_date`,`passangers`,`from`,`to`,`locations`,`return`,`return_time`,`hours`,`job_type` FROM `jobs` WHERE `id`=$temp "; //AND `start_date`< NOW()
					$elapse = $this->db->query($sql);	
					$trips= $elapse->result();
					$count_elapse = count($trips);
						
					if($count_elapse >0){
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Job has been added to database!'));
						echo json_encode($arr);
						for($i=0; $i<$count_elapse; $i++){
							$id = $trips[$i];
							if($id->job_type == 0 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Trip Base','start_date'=>$id->start_date,'passangers'=>$id->passangers,'from'=>$id->from,'to'=>$id->to,'return'=>$id->return,'return_time'=>$id->return_time,);
							}
							else if($id->job_type == 1 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Time Base','start_date'=>$id->start_date,'end_date'=>$id->end_date,'passangers'=>$id->passangers,'locations'=>$id->locations,'hours'=>$id->hours);
							}
							else if($id->job_type == 2 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Term Base','start_date'=>$id->start_date);
							}
						}		
						$string = json_encode($arr);
						$string = str_replace( '\"','"' , $string);
						$string = str_replace( '"[','[' , $string);
						$string = str_replace( ']"',']' , $string);
						echo ('{"List job": '.$string.'}');
					}
					else{
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
						echo json_encode($arr);
					}					
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
		}
		}
		
		// edit trip base
		function edit_trip_base()
		{ 
		if($this->Simulate==1){
			echo '{"id":"249","type":"Trip Base","start_date":"2013-11-15 18:30:13","passangers":"1234","from":"24","to":"["4124"]","return":"23","return_time":"2013-11-13 19:30:13"}{"Status":"ok","result":{"message":"Job has been add to database!"}}';
		}
		else{
			header('Content-type: application/json');
			/*$body_request =(string)('{
				"requestBody": [
					{
						"type": "tripBase",
						"startDate": "2012-12-30 12:00:00",
						"numberOfPassengers": 5,
						"from": "Binh Duong",
						"to": [
							"Vung Tau",
							"Nha Trag",
							"TPHCM"
						],
						"returnLocation": "Binh Duong",
						"returnTime": "2012-12-31 12:00:00"
					}
				]
			}');*/
			$body_request = file_get_contents('php://input');
			$temp = explode('{',$body_request);
			$temp1 = explode('}',$temp[2]);
			$temp2 = explode('"',$temp1[0]);
			$startdate = $temp2[7];
			$temp3 = explode(' ',$temp2[10]);
			$temp4 = explode(',',$temp3[1]);
			$numofpas  = $temp4[0];
			$from= $temp2[13];
			$temp5 = explode('[',$temp1[0]);
			$temp6 = explode(']',$temp5[1]);
			$locations = '['.$temp6[0].']';
			$locations = str_replace("\t","",$locations);
			$locations = str_replace("\r","",$locations);
			$to = str_replace("\n","",$locations);
			$temp7 = explode('"',$temp6[1]);
			$returnplace   = $temp7[3];
			$returntime   = $temp7[7];
			/*$startdate = @$_REQUEST['startDate'];
			$returntime  = @$_REQUEST['returnTime'];
			$numofpas  = @$_REQUEST['numberOfPassengers'];
			$from = @$_REQUEST['from'];
			$to = @$_REQUEST['to'];
			$returnplace = @$_REQUEST['returnLocation'];*/
			$arr;
			if (true){
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');

				
				$userid =@$_REQUEST['userid'];
				$jobid =@$_REQUEST['jobid'];
				$jobgroupid = @$_REQUEST['jobgroupid'];
				if(preg_match("/^[0-9]+$/", $userid)){
					$sql = "SELECT * FROM `job_group` WHERE `owner_id`=".$userid ." AND `gid`=".$jobgroupid;
					$elapse1 = $this->db->query($sql);	
					$user_flag= $elapse1->result();
					$count_elapse1 = count($user_flag);
					$sql = "SELECT * FROM `jobs` WHERE `owner_id`=".$userid ." AND `belongs`=".$jobgroupid." AND `id`=".$jobid." AND `job_type`=0";
					$elapse2 = $this->db->query($sql);	
					$user_flag2= $elapse2->result();
					$count_elapse2 = count($user_flag2);
					if($count_elapse1 >0){
						if($count_elapse2 >0){
							$sql = "UPDATE `jobs` SET `start_date`='$startdate',`return_time`='$returntime',`passangers`=".$numofpas.",`from`='$from',`to`='$to',`owner_id`=".$userid.",`return`='$returnplace',`belongs`=".$jobgroupid." WHERE `id`=".$jobid;
							$elapse = $this->db->query($sql);	
						}
						else{
							$arr = array ('Status'=>'False','result'=>array('message'=>'This job is not trip base. Try with another JobID'));
							echo json_encode($arr);	
							return 0;
						}
						
					}
					else{
						$arr = array ('Status'=>'False','result'=>array('message'=>'UserID or JobID or JobGroupID is not correct!'));
						echo json_encode($arr);			
					}
					
					$sql = "SELECT `id`,`start_date`,`end_date`,`passangers`,`from`,`to`,`locations`,`return`,`return_time`,`hours`,`job_type` FROM `jobs` WHERE `id`=".$jobid; //AND `start_date`< NOW()
					$elapse = $this->db->query($sql);	
					$trips= $elapse->result();
					$count_elapse = count($trips);
						
					if($count_elapse >0){
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Job has been updated to database!'));
						echo json_encode($arr);	
						for($i=0; $i<$count_elapse; $i++){
							$id = $trips[$i];
							if($id->job_type == 0 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Trip Base','start_date'=>$id->start_date,'passangers'=>$id->passangers,'from'=>$id->from,'to'=>$id->to,'return'=>$id->return,'return_time'=>$id->return_time,);
							}
							else if($id->job_type == 1 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Time Base','start_date'=>$id->start_date,'end_date'=>$id->end_date,'passangers'=>$id->passangers,'locations'=>$id->locations,'hours'=>$id->hours);
							}
							else if($id->job_type == 2 ){
								$arr[$i] = array ('id'=>$id->id,'type'=>'Term Base','start_date'=>$id->start_date);
							}
						}	
						$string = json_encode($arr);
						$string = str_replace( '\"','"' , $string);
						$string = str_replace( '"[','[' , $string);
						$string = str_replace( ']"',']' , $string);
						echo ('{"List job": '.$string.'}');
					}
					else{
						$arr = array ('Status'=>'ok','result'=>array('message'=>'Have no matching trip!'));
						echo json_encode($arr);
					}
					
				}
				else{
					$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
					echo json_encode($arr);
				}
			}
			
		}
		}
		
		// create a new job group
		function create_job_group()
		{   
		if($this->Simulate==1){
			$arr = array ('Status'=>'ok');
			echo json_encode($arr);
		}
		else{
			header('Content-type: application/json');
			$s = file_get_contents('php://input');

			if (true){
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');
				$userid = @$_REQUEST['userid'];
				$sql = "INSERT INTO `job_group` (`owner_id`) VALUES (".$userid.")" ;
				$elapse1 = $this->db->query($sql);		
				$sql = "SELECT `gid` FROM `job_group` ORDER BY `gid` DESC";
				$elapse = $this->db->query($sql);	
				$trips= $elapse->result();
				$jobgroupid = $trips[0]->gid;
				
				
				$temp = explode('{',$s);
				$count_temp = count($temp);
				for ( $i = 2 ; $i < $count_temp ; $i++ ){
					$temp1 = explode('}',$temp[$i]);
					$temp2 = explode('"',$temp1[0]);
					if($temp2[3]=="timeBase"){
						$startdate = $temp2[7];
						$enddate   = $temp2[11];
						$temp3 = explode(' ',$temp2[14]);
						$temp4 = explode(',',$temp3[1]);
						$numofpas  = $temp4[0];
						$temp5 = explode('[',$temp1[0]);
						$locations = '['.$temp5[1];
						$locations = str_replace("\t","",$locations);
						$locations = str_replace("\r","",$locations);
						$locations = str_replace("\n","",$locations);
						$numofhour = $this->diffhour($startdate , $enddate);
						if (true){
						
							$this->load->helper('form');		
							$user =& Users::getInstance();		
							
							$this->load->helper('url');
							$this->load->helper('form');
							$this->load->library('Menu');
							$this->load->helper('security');
							$this->load->library('email');

							
							
							if(preg_match("/^[0-9]+$/", $userid)){
								if(true){
									$sql = "INSERT INTO `jobs` (`start_date`,`end_date`,`passangers`,`hours`,`locations`,`owner_id`,`job_type`,`belongs`) VALUES ('$startdate','$enddate',".$numofpas.",".$numofhour.",'$locations',".$userid.",1,".$jobgroupid.")" ;
									$elapse = $this->db->query($sql);
								}		
							}
							else{
								$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
								echo json_encode($arr);
							}
						}
					}
					else if($temp2[3]=='tripBase'){
						$startdate = $temp2[7];
						$temp3 = explode(' ',$temp2[10]);
						$temp4 = explode(',',$temp3[1]);
						$numofpas  = $temp4[0];
						$from= $temp2[13];
						$temp5 = explode('[',$temp1[0]);
						$temp6 = explode(']',$temp5[1]);
						$locations = '['.$temp6[0].']';
						$locations = str_replace("\t","",$locations);
						$locations = str_replace("\r","",$locations);
						$to = str_replace("\n","",$locations);
						$temp7 = explode('"',$temp6[1]);
						$returnplace   = $temp7[3];
						$returntime   = $temp7[7];
						$arr;
						if (true){
						
							$this->load->helper('form');		
							$user =& Users::getInstance();		
							
							$this->load->helper('url');
							$this->load->helper('form');
							$this->load->library('Menu');
							$this->load->helper('security');
							$this->load->library('email');
						
							if(preg_match("/^[0-9]+$/", $userid)){
								if(true){
									$sql = "INSERT INTO `jobs` (`start_date`,`end_date`,`return_time`,`passangers`,`from`,`to`,`owner_id`,`job_type`,`belongs`,`return`) VALUES ('$startdate','$returntime','$returntime',".$numofpas.",'$from','$to',".$userid.",0,".$jobgroupid.",'$returnplace')" ;
									$elapse = $this->db->query($sql);
								}
							}
							else{
								$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
								echo json_encode($arr);
							}
						}
						
					}
				}
				$this->Jobs->CalcStartDate($jobgroupid);
				$this->Jobs->CalcEndDate($jobgroupid);
				$create_date = date("Y-m-d H:i:s", time());
				$sessionid =  $this->session->userdata('session_id');
				$sql = "UPDATE `job_group` SET `create_date` = '$create_date', `sessid`= '$sessionid' WHERE `gid`=$jobgroupid "; //AND `start_date`< NOW()
				$elapse = $this->db->query($sql);
				$arr = array ('Status'=>'ok');
				echo json_encode($arr);
			}
		}
		}
		function save_draft()
		{   
		if($this->Simulate==1){
			$arr = array ('Status'=>'ok');
			echo json_encode($arr);
		}
		else{
			header('Content-type: application/json');
			$s = file_get_contents('php://input');

			if (true){
			
				$this->load->helper('form');		
				$user =& Users::getInstance();		
				$this->load->helper('url');
				$this->load->helper('form');
				$this->load->library('Menu');
				$this->load->helper('security');
				$this->load->library('email');
				$userid = @$_REQUEST['userid'];
				$sql = "INSERT INTO `job_group` (`owner_id`) VALUES (".$userid.")" ;
				$elapse1 = $this->db->query($sql);		
				$sql = "SELECT `gid` FROM `job_group` ORDER BY `gid` DESC";
				$elapse = $this->db->query($sql);	
				$trips= $elapse->result();
				$jobgroupid = $trips[0]->gid;
				
				
				$temp = explode('{',$s);
				$count_temp = count($temp);
				for ( $i = 2 ; $i < $count_temp ; $i++ ){
					$temp1 = explode('}',$temp[$i]);
					$temp2 = explode('"',$temp1[0]);
					if($temp2[3]=="timeBase"){
						$startdate = $temp2[7];
						$enddate   = $temp2[11];
						$temp3 = explode(' ',$temp2[14]);
						$temp4 = explode(',',$temp3[1]);
						$numofpas  = $temp4[0];
						$temp5 = explode('[',$temp1[0]);
						$locations = '['.$temp5[1];
						$locations = str_replace("\t","",$locations);
						$locations = str_replace("\r","",$locations);
						$locations = str_replace("\n","",$locations);
						$numofhour = $this->diffhour($startdate , $enddate);
						if (true){
						
							$this->load->helper('form');		
							$user =& Users::getInstance();		
							
							$this->load->helper('url');
							$this->load->helper('form');
							$this->load->library('Menu');
							$this->load->helper('security');
							$this->load->library('email');

							
							
							if(preg_match("/^[0-9]+$/", $userid)){
								if(true){
									$sql = "INSERT INTO `jobs` (`start_date`,`end_date`,`passangers`,`hours`,`locations`,`owner_id`,`job_type`,`belongs`) VALUES ('$startdate','$enddate',".$numofpas.",".$numofhour.",'$locations',".$userid.",1,".$jobgroupid.")" ;
									$elapse = $this->db->query($sql);
								}		
							}
							else{
								$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
								echo json_encode($arr);
							}
						}
					}
					else if($temp2[3]=='tripBase'){
						$startdate = $temp2[7];
						$temp3 = explode(' ',$temp2[10]);
						$temp4 = explode(',',$temp3[1]);
						$numofpas  = $temp4[0];
						$from= $temp2[13];
						$temp5 = explode('[',$temp1[0]);
						$temp6 = explode(']',$temp5[1]);
						$locations = '['.$temp6[0].']';
						$locations = str_replace("\t","",$locations);
						$locations = str_replace("\r","",$locations);
						$to = str_replace("\n","",$locations);
						$temp7 = explode('"',$temp6[1]);
						$returnplace   = $temp7[3];
						$returntime   = $temp7[7];
						$arr;
						if (true){
						
							$this->load->helper('form');		
							$user =& Users::getInstance();		
							
							$this->load->helper('url');
							$this->load->helper('form');
							$this->load->library('Menu');
							$this->load->helper('security');
							$this->load->library('email');
						
							if(preg_match("/^[0-9]+$/", $userid)){
								if(true){
									$sql = "INSERT INTO `jobs` (`start_date`,`end_date`,`return_time`,`passangers`,`from`,`to`,`owner_id`,`job_type`,`belongs`,`return`) VALUES ('$startdate','$returntime','$returntime',".$numofpas.",'$from','$to',".$userid.",0,".$jobgroupid.",'$returnplace')" ;
									$elapse = $this->db->query($sql);
								}
							}
							else{
								$arr = array ('Status'=>'false','result'=>array('message'=>'UserID and JobGroupID have to be integers'));
								echo json_encode($arr);
							}
						}
						
					}
				}
				$this->Jobs->CalcStartDate($jobgroupid);
				$this->Jobs->CalcEndDate($jobgroupid);
				$create_date = date("Y-m-d H:i:s", time());
				$sessionid =  $this->session->userdata('session_id');
				$sql = "UPDATE `job_group` SET `draft` = 1, `create_date` = '$create_date', `sessid`= '$sessionid' WHERE `gid`=$jobgroupid "; //AND `start_date`< NOW()
				$elapse = $this->db->query($sql);
				$arr = array ('Status'=>'ok');
				echo json_encode($arr);
			}
		}
		}
		function diffhour($s1,$s2){

			$ymd = explode(' ',$s1);
			$ymd1 = explode('-',$ymd[0]);
			$ymd2 = explode(':',$ymd[1]);
			$start = mktime($ymd2[0],$ymd2[1],$ymd2[2],$ymd1[1],$ymd1[2],$ymd1[0]);
			$eymd = explode(' ',$s2);
			$eymd1 = explode('-',$eymd[0]);
			$eymd2 = explode(':',$eymd[1]);
			$end = mktime($eymd2[0],$eymd2[1],$eymd2[2],$eymd1[1],$eymd1[2],$eymd1[0]);
			$result = ($end - $start)/3600;
			return $result;
		
		}
		

	}
?>
